<?php
require("libraries/student.php");
class Mini extends Student{
    public function __construct(){
        parent::__construct();
        $this->load->Model("mmini");
        $this->load->library("string");
    }
    public function index(){
        $config['base_url'] = base_url().'admin/mini/index/';
        $config['total_rows'] = $this->mmini->count_all();
        $config['per_page'] = 10;
        $config['uri_segment'] = 4;
        $config['next_link'] = "Next";
        $config['prev_link'] = "Prev";
        $config['first_link'] = "First";
        $config['last_link'] = "Last";
        $this->load->library("pagination");
        $this->pagination->initialize($config);
        $start = $this->uri->segment(4);
        $data['title']="Mini packages";
        $data['act'] = 6;
        $data['data']['info']= $this->mmini->listall($config['per_page'],$start);
        $data['template'] = "mini/list_mini";
        $this->load->view("layout",$data);
    }
    public function add(){
        $this->load->helper(array("form","date"));
        $this->load->library("form_validation");
        $data['error'] = "";
        $data['data'] = "";
        $data['act'] = 6;
        $data['title'] = "Mini packages";
        $data['template'] = "mini/add_mini";
        //$data['listcago'] = $this->mmini->listcago();
        if($this->input->post("ok") != ""){
            $this->form_validation->set_rules("name_hotel","Name Hotel","min_length[5]|callback_check_mini");
            if($this->form_validation->run() == FALSE){
                $this->load->view("layout",$data);
            }else{
                $db = array(
                    "name_hotel" => $this->input->post("name_hotel"),
                    "sao_hotel" => $this->input->post("sao_hotel"),
                    "title_hotel" => $this->input->post("title_hotel"),
                    "name_tau" => $this->input->post("name_tau"),
                    "sao_tau" => $this->input->post("sao_tau"),
                    "title_tau" => $this->input->post("title_tau"),
                    "gia" => $this->input->post("gia"),
                    "gia_tru" => $this->input->post("gia_tru"),
                    "mini_include" => $this->input->post("mini_include"),
                    "mini_date"=>time(),
                    "mini_status"=> 0
                );
                if($_FILES['img1']['name'] != NULL){
                    $config['upload_path'] = './uploads/minis';
                    $config['allowed_types'] = 'gif|jpg|jpeg|png';
                    $config['encrypt_name'] = true;
                    $config['max_size']	= '2000';
                    $config['max_width']  = '1400';
                    $config['max_height']  = '1400';
                    $this->load->library('upload',$config);
                    if(!$this->upload->do_upload("img1")){
                        $data = array('error' => $this->upload->display_errors());
                        $data['data'] = "";
                        $data['title'] = "Mini packages";
                        $data['template'] = "mini/add_mini";
                        $this->load->view("layout",$data);
                        return FALSE;
                    }else{
                        $data = $this->upload->data();
                        $db['img_hotel'] = $data['file_name'];
                        $this->createThumbnail("uploads/minis",$db['img_hotel'],175,120);
                    }
                }
                if($_FILES['img2']['name'] != NULL){
                    $config['upload_path'] = './uploads/minis';
                    $config['allowed_types'] = 'gif|jpg|jpeg|png';
                    $config['encrypt_name'] = true;
                    $config['max_size']	= '2000';
                    $config['max_width']  = '1400';
                    $config['max_height']  = '1400';
                    $this->load->library('upload',$config);
                    if(!$this->upload->do_upload("img2")){
                        $data = array('error' => $this->upload->display_errors());
                        $data['data'] = "";
                        $data['title'] = "Mini packages";
                        $data['template'] = "mini/add_mini";
                        $this->load->view("layout",$data);
                        return FALSE;
                    }else{
                        $data = $this->upload->data();
                        $db['img_tau'] = $data['file_name'];
                        $this->createThumbnail("uploads/minis",$db['img_tau'],175,120);
                    }
                }
                $this->mmini->add($db);
                redirect(base_url()."admin/mini/index");
            }
        }else{
            $this->load->view("layout",$data);
        }
    }

    public function update(){
        $id = $this->uri->segment(4);
        $this->load->helper(array("form","date"));
        $this->load->library("form_validation");
        $data['error'] = "";
        $data['title'] = "Mini packages";
        $data['template'] = "mini/edit_mini";
        $data['data'] = "";
        $data['act'] = 6;
        $data['get'] = $this->mmini->getOne($id);
        if($this->input->post("ok") != ""){
            $this->form_validation->set_rules("name_hotel","Name Hotel","min_length[5]|callback_check_mini");
            if($this->form_validation->run() == FALSE){
                $this->load->view("layout",$data);
            }else{
                $db = array(
                    "name_hotel" => $this->input->post("name_hotel"),
                    "sao_hotel" => $this->input->post("sao_hotel"),
                    "title_hotel" => $this->input->post("title_hotel"),
                    "name_tau" => $this->input->post("name_tau"),
                    "sao_tau" => $this->input->post("sao_tau"),
                    "title_tau" => $this->input->post("title_tau"),
                    "gia" => $this->input->post("gia"),
                    "gia_tru" => $this->input->post("gia_tru"),
                    "mini_include" => $this->input->post("mini_include"),
                    "mini_date"=>time(),
                    "mini_status"=> 0
                );
                if($_FILES['img1']['name'] != NULL){
                    $config['upload_path'] = './uploads/minis';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['encrypt_name'] = true;
                    $config['max_size']	= '2000';
                    $config['max_width']  = '1400';
                    $config['max_height']  = '1400';
                    $this->load->library('upload',$config);
                    if(!$this->upload->do_upload("img1")){
                        $data = array('error' => $this->upload->display_errors());
                        $data['title'] = "Mini packages";
                        $data['template'] = "mini/edit_mini";
                        $data['data'] = "";
                        $data['act'] = 6;
                        $data['get'] = $this->mmini->getOne($id);
                        $this->load->view("layout",$data);
                        return FALSE;
                    }else{
                        $data = $this->upload->data();
                        $db['img_hotel'] = $data['file_name'];
                        $this->createThumbnail("uploads/minis",$db['img_hotel'],180,130);
                    }
                }
                if($_FILES['img2']['name'] != NULL){
                    $config['upload_path'] = './uploads/minis';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['encrypt_name'] = true;
                    $config['max_size']	= '2000';
                    $config['max_width']  = '1400';
                    $config['max_height']  = '1400';
                    $this->load->library('upload',$config);
                    if(!$this->upload->do_upload("img2")){
                        $data = array('error' => $this->upload->display_errors());
                        $data['title'] = "Mini packages";
                        $data['template'] = "mini/edit_mini";
                        $data['data'] = "";
                        $data['act'] = 4;
                        $data['get'] = $this->mmini->getOne($id);
                        $this->load->view("layout",$data);
                        return FALSE;
                    }else{
                        $data = $this->upload->data();
                        $db['img_tau'] = $data['file_name'];
                        $this->createThumbnail("uploads/minis",$db['img_tau'],180,130);
                    }
                }
                $this->mmini->update($db,$id);
                redirect(base_url()."admin/mini/index");
            }
        }else{
            $this->load->view("layout",$data);
        }
    }
    public function search(){
        $title = $this->input->post("val");
        if($title == NULL){
            echo "Bạn phải nhập tiêu đề tin!";
        }else{
            $data  = $this->mmini->search_ajax($title);
            if($data == NULL){
                echo "Không tìm thấy tin tức nào!";
            }else{
                echo '<table cellpadding="0" cellspacing="0" width="905">
									<tbody><tr>
									  <th width="23">Stt.</th>
									  <th width="300">Tiêu đề tin</th>
									  <th width="136">Tác giả</th>
									  <th width="132">Ngày đăng</th>
									  <th width="64">Actions</th>
									</tr>';
                $stt = 0;
                foreach($data as $items){
                    $stt++;
                    echo "<tr>";
                    echo "<td>$stt</td>";
                    echo "<td>".$items['news_title']."</td>";
                    echo "<td>".$items['news_author']."</td>";
                    echo "<td>".$items['news_date']."</td>";
                    echo "<td>";
                    echo "<div class='actions_menu'><ul>";
                    echo "<li><a href='".base_url()."admin/news/update/$items[news_id]/' class='edit'>Edit</a></li>";
                    echo "<li><a href='".base_url()."admin/news/del/$items[news_id]/' class='delete' onclick='return check()'>Del</a></li>";
                    echo "</ul></div>";
                    echo "</td>";

                    echo "</tr>";
                }
                echo "</tbody></table>";
            }
        }
    }
    public function del(){
        $id = $this->uri->segment(4);
        $data = $this->mmini->getOne($id);
        @unlink("uploads/minis/".$data['img_hotel']);
        @unlink("uploads/minis/".$data['img_tau']);
        $this->mmini->del($id);
        redirect(base_url()."admin/news");
    }
    public function createThumbnail($parth,$fileName,$width,$height){
        $this->load->library('image_lib');
        //$this->load->helper('thumbnail_helper');
        $config['image_library'] = 'gd2';
        $config['source_image'] = $parth.'/'.$fileName;
        $config['new_image'] = $parth.'/thumb/'.$fileName;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['thumb_marker'] = FALSE;
        $config['width'] = $width;
        $config['height'] = $height;
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        $this->image_lib->clear();
    }
}